import os

from lib import common
from TcpClient import tcpclient
import struct, json
from conf import settings


user_dic = {
	"cookie": None
}


def register(client):
	while 1:
		user_name = input("user_name:>>").strip()
		password = input("password:>>").strip()
		password_again = input("password_again:>>").strip()
		if password == password_again:
			send_dic = {
				'type': "register",
				'name': user_name,
				'password': common.get_password(password),
				'user_type': "admin"
			}
			back_dic = common.send_back(client, send_dic)
			if back_dic['flag']:
				print(back_dic['msg'])
				break
			else:
				print(back_dic['msg'])
		else:
			print("两次密码不一致...")


def login(client):
	while 1:
		user_name = input("user_name:>>").strip()
		if user_name == 'q':break
		password = input("password:>>").strip()
		if password == 'q':break
		send_dic = {
			'type': "login",
			'name': user_name,
			'password': common.get_password(password),
			'user_type': "admin"
		}
		back_dic = common.send_back(client, send_dic)
		if back_dic['flag']:
			print(back_dic['msg'])
			user_dic['cookie'] = back_dic['session']
			break
		else:
			print(back_dic['msg'])


def upload_movie(client):
	while 1:
		movie_list = common.get_movie_list()
		# print(movie_list)
		for index, v in enumerate(movie_list, 1):
			print("%s-->%s" % (index, v))
		choice_movie = input("please input you upload movie number:>>").strip()
		if choice_movie == 'q':break
		if choice_movie.isdigit():
			choice_movie = int(choice_movie)
			# 在指定范围内的话
			if choice_movie in range(1, len(movie_list) + 1):
				movie_name = movie_list[choice_movie - 1]
				# print(movie_name)
				movie_md5 = common.get_movie_md5(movie_name)
				send_dic = {'type': "check_movie", "file_md5": movie_md5, "session": user_dic['cookie']}
				back_dic = common.send_back(client, send_dic)
				if back_dic['flag']:  # 判断视频文件是否存在,如果为True的话就继续进行上传
					print(back_dic['msg'])
					is_free = input('电影是否免费(y/n):>>').strip()
					is_free = 1 if is_free == 'y' else 0
					movie_path = os.path.join(settings.BASE_UPLOAD_MOVIE, movie_name)
					file_size = os.path.getsize(movie_path)
					send_dic = {"type": "upload_movie", "path": movie_path, "movie_size": file_size,  "session": user_dic['cookie'], "name": movie_name, "file_md5": movie_md5, "is_free": is_free}
					back_dic = common.send_back(client, send_dic, movie_path)
					if back_dic["flag"]:
						print(back_dic["msg"])
						break
					else:
						print(back_dic['msg'])
				else:
					print(back_dic['msg']),

			else:
				print('请输入在范围内的序号...')
		else:
			print('请输入序号...')


def delete_movie(client):
	while 1:
		movie_list = common.get_movie_list()
		for index, v in enumerate(movie_list, 1):
			print("%s-->%s" % (index, v))
		choice = input("please input you want delete movie number:>>").strip()
		if choice == 'q':break
		if choice.isdigit():
			choice = int(choice)
			if choice in range(1, len(movie_list) + 1):
				movie_name = movie_list[choice - 1]
				# print(movie_name)
				movie_md5 = common.get_movie_md5(movie_name)
				send_dic = {'type': "check_movie", "file_md5": movie_md5, "session": user_dic['cookie']}
				back_dic = common.send_back(client, send_dic)
				# print(back_dic)
				if not back_dic['flag']:  # 代表电影已经存在
					path = os.path.join(settings.BASE_UPLOAD_MOVIE, movie_name)
					send_dic = {"type": "delete_movie", "session": user_dic['cookie'], 'name': movie_name, "path": path}
					back_dic = common.send_back(client, send_dic)
					if back_dic["flag"]:
						print(back_dic["msg"])
						break
					else:
						print(back_dic["msg"])
				else:
					print('电影还未上传，无法进行删除...')
			else:
				print('请输入在范围内的序号...')
		else:
			print('类型必须是数字类型...')


def release_notice(client):
	pass


func_dic = {
	'1': register,
	'2': login,
	'3': upload_movie,
	'4': delete_movie,
	'5': release_notice
}


def admin_view():
	client = tcpclient.get_client()
	while True:
		print("""
			1、注册
			2、登陆
			3、上传视频
			4、删除视频
			5、发布公告
		""")
		choice = input("请输入你的选择:>>").strip()
		if choice == 'q':break
		if choice not in func_dic:continue
		func_dic[choice](client)

